package com.leiusual.mall_ad

import android.content.Context
import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import com.fighter.loader.listener.SplashViewListener
import com.fighter.loader.view.AdViewCreator
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView
import org.json.JSONObject
import kotlin.concurrent.thread

/**
 * author: leiusual
 * create on: 2020/10/28
 * description:
 */
class MallAdView(
    private val context: Context,
    id: Int,
    messenger: BinaryMessenger
) : PlatformView, MethodChannel.MethodCallHandler {

  companion object {
    private val TAG = MallAdView::class.java.simpleName
  }

  private val frameLayout = FrameLayout(context)
  private val methodChannel = MethodChannel(messenger, "com.leiusual.mall_ad/view_$id")

  init {
    Log.d(TAG, "init")
    frameLayout.setBackgroundColor(Color.BLUE)
    methodChannel.setMethodCallHandler(this)
  }

  override fun getView(): View {
    Log.d(TAG, "getView")
    return frameLayout
  }

  override fun dispose() {
    Log.d(TAG, "dispose")
    methodChannel.setMethodCallHandler(null)
  }

  override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    when (call.method) {
      "fetchAd" -> {
        thread {
          try {
            fetchAd()
          } catch (e: Exception) {
            e.printStackTrace()
          }
        }
        result.success(null)
      }
      else -> {
        result.notImplemented()
      }
    }
  }

  private fun fetchAd() {
    Log.d(TAG, "fetchAd")

    val adViewCreator = AdViewCreator.getInstance(context.applicationContext)
    val builder = adViewCreator.createSplashViewBuilder()
    builder.setActivity(MallAdPlugin.activity)
    builder.setReaperApi(MallAdInitializer.reaperApi)
    builder.setPosId("1580")
    builder.setContainer(frameLayout)
    builder.setListener(object : SplashViewListener {
      override fun onSplashAdClick() {
        Log.d(TAG, "onSplashAdClick")
        methodChannel.invokeMethod("onSplashAdClick", null)
      }

      override fun onJumpClicked() {
        Log.d(TAG, "onJumpClicked")
        methodChannel.invokeMethod("onJumpClicked", null)
      }

      override fun onSplashAdDismiss() {
        Log.d(TAG, "onSplashAdDismiss")
        methodChannel.invokeMethod("onSplashAdDismiss", null)
      }

      override fun onSplashAdFailed(p0: String?) {
        Log.d(TAG, "onSplashAdFailed")
        methodChannel.invokeMethod("onSplashAdFailed", p0)
      }

      override fun onSplashAdPresent() {
        Log.d(TAG, "onSplashAdPresent")
        methodChannel.invokeMethod("onSplashAdPresent", null)
      }

      override fun onSplashAdShow() {
        Log.d(TAG, "onSplashAdShow")
        methodChannel.invokeMethod("onSplashAdShow", null)
      }

      override fun onAdInfo(p0: JSONObject?) {
        Log.d(TAG, "onAdInfo")
        methodChannel.invokeMethod("onAdInfo", p0)
      }
    })

    val splashView = builder.build()
    splashView.create()
  }
}